# K8s Deployment Resource Extractor - PowerShell Launch Script

# Get script directory
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location -Path $ScriptDir

# Check Python environment
if (-not (Get-Command python -ErrorAction SilentlyContinue)) {
    Write-Host "Error: Python command not found. Please ensure Python is installed and added to PATH" -ForegroundColor Red
    exit 1
}

# Initialize variables
$Interactive = $false
$ConfigFile = "config.yaml"
$SourceDir = "sources"
$OutputDir = "outputs"
$Verbose = $false
$ForceRun = $false

# Parse command-line arguments
$i = 0
while ($i -lt $args.Count) {
    switch ($args[$i]) {
        "-i" { $Interactive = $true }
        "--interactive" { $Interactive = $true }
        "-c" { $ConfigFile = $args[$i+1]; $i++ }
        "--config" { $ConfigFile = $args[$i+1]; $i++ }
        "-s" { $SourceDir = $args[$i+1]; $i++ }
        "--source" { $SourceDir = $args[$i+1]; $i++ }
        "-o" { $OutputDir = $args[$i+1]; $i++ }
        "--output" { $OutputDir = $args[$i+1]; $i++ }
        "-v" { $Verbose = $true }
        "--verbose" { $Verbose = $true }
        "-y" { $ForceRun = $true }
        "--yes" { $ForceRun = $true }
        default { Write-Host "Unknown argument: $($args[$i])" -ForegroundColor Yellow }
    }
    $i++
}

# Interactive mode
if ($Interactive -and (-not $ForceRun)) {
    Write-Host "Welcome to K8s Deployment Resource Extractor" -ForegroundColor Cyan
    Write-Host "===========================================" -ForegroundColor Cyan
    
    # Source directory
    $SourceInput = Read-Host "Source directory [$SourceDir]"
    if ($SourceInput -ne '') {
        $SourceDir = $SourceInput
    }
    
    # Output directory
    $OutputInput = Read-Host "Output directory [$OutputDir]"
    if ($OutputInput -ne '') {
        $OutputDir = $OutputInput
    }
    
    # Config file
    $ConfigInput = Read-Host "Configuration file [$ConfigFile]"
    if ($ConfigInput -ne '') {
        $ConfigFile = $ConfigInput
    }
    
    # Verbose mode
    $VerboseInput = Read-Host "Enable verbose logging? (y/N)"
    if ($VerboseInput -eq 'y' -or $VerboseInput -eq 'Y') {
        $Verbose = $true
    }
    
    # Show summary
    Write-Host "`nSettings Summary:" -ForegroundColor Cyan
    Write-Host "  Source Directory: $SourceDir"
    Write-Host "  Output Directory: $OutputDir"
    Write-Host "  Configuration: $ConfigFile"
    Write-Host "  Verbose: $(if ($Verbose) { 'Yes' } else { 'No' })"
    
    # Final confirmation
    $ConfirmInput = Read-Host "`nStart extraction with these settings? (Y/n)"
    if ($ConfirmInput -eq 'n' -or $ConfirmInput -eq 'N') {
        Write-Host "Extraction cancelled by user" -ForegroundColor Yellow
        exit 0
    }
}

# Build arguments string
$Arguments = ""
$Arguments += " --source `"$SourceDir`""
$Arguments += " --output `"$OutputDir`""
$Arguments += " --config `"$ConfigFile`""
if ($Verbose) {
    $Arguments += " --verbose"
}

# Start the application
Write-Host "Starting K8s Deployment Resource Extractor..." -ForegroundColor Cyan
python -m src $Arguments

# Check exit status
if ($LASTEXITCODE -ne 0) {
    Write-Host "Application exited with error code: $LASTEXITCODE" -ForegroundColor Red
    exit $LASTEXITCODE
}

exit 0
